package com.pheruagor.handmanager.model {
    import com.pheruagor.handmanager.model.business.Club;
    import com.pheruagor.handmanager.model.business.Member;
    import com.pheruagor.handmanager.storage.impl.sqlite.MemberSQLiteStorage;

    import mx.collections.ArrayCollection;

    import org.puremvc.as3.patterns.proxy.Proxy;

    public class ClubViewerProxy extends Proxy {
        public static const NAME:String = "clubViewerProxy";

        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_currentClub:Club;

        private var m_memberStorage:MemberSQLiteStorage;

        //private var m_memberStorage:
        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function ClubViewerProxy(club:Club) {
            super(NAME, club);
            m_currentClub = club.clone();
            if (m_currentClub.members == null) {
                m_currentClub.members = new ArrayCollection();
            }
        }


        //-----------------------------------------------------
        // IProxy Interface
        //-----------------------------------------------------
        override public function onRegister():void {
            m_memberStorage = new MemberSQLiteStorage();
            initializeMembers();
        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        public function get currentClub():Club {
            return m_currentClub;
        }

        public function get originalClub():Club {
            return Club(data);
        }


        //-----------------------------------------------------
        // Operation
        //-----------------------------------------------------

        public function saveMember(member:Member, onResult:Function = null, onFault:Function = null):void {
            member.clubId = m_currentClub.id;
            var resultFct:Function = function():void {
                    updateOrAddMember(member);
                    if (onResult != null) {
                        onResult(member);
                    }

                }
            m_memberStorage.saveMember(member, resultFct);
        }

        public function removeMember(member:Member, onResult:Function = null, onFault:Function = null):void {
            var fctResult:Function = function():void {
                    m_currentClub.members.removeItemAt(m_currentClub.members.getItemIndex(member));
                    if (onResult != null) {
                        onResult();
                    }
                };
            m_memberStorage.removeMember(member, fctResult);
        }

        //-----------------------------------------------------
        // Implementation
        //-----------------------------------------------------
        private function initializeMembers():void {
            var onResult:Function = function(result:Array):void {
                    for each (var member:Member in result) {
                        m_currentClub.members.addItem(member);
                    }
                };
            m_memberStorage.listMembers(m_currentClub.id, onResult);
        }

        private function updateOrAddMember(member:Member):void {
            for each (var currentMember:Member in m_currentClub.members) {
                if (currentMember.id == member.id) {
                    m_currentClub.members.removeItemAt(m_currentClub.members.getItemIndex(currentMember));
                    m_currentClub.members.addItem(member);
                    return;
                }
            }
            m_currentClub.members.addItem(member);
        }
    }
}